<template>
	<div class="releaselog">
		<Nav></Nav>
		<div class="writing_right">
			<div class="TitleList">
				<div class="TitleList_padding">
					<div class="TitleList_top">
						<div class="TitleList_top_title"><span></span>发布日志</div>
						<div class="TitleList_top_txt">
							<div class="top_txt_list time">
								<el-date-picker v-model="params.time" type="daterange" align="right" unlink-panels
									format="yyyy-MM-dd" value-format="yyyy-MM-dd" range-separator="至"
									start-placeholder="开始日期" end-placeholder="结束日期">
								</el-date-picker>
							</div>
							<div class="top_txt_list">
								<el-select clearable v-model="params.article_class_id" placeholder="所属分类">
									<el-option v-for="item in class_types" :key="item.id" :label="item.name"
										:value="item.id">
									</el-option>
								</el-select>
							</div>
							<div class="top_txt_list">
								<el-select clearable v-model="params.push_plan_id" placeholder="所属计划">
									<el-option v-for="item in plans" :key="item.id" :label="item.name" :value="item.id">
									</el-option>
								</el-select>
							</div>
							<div class="top_txt_list">
								<el-select clearable v-model="params.site_id" placeholder="所属站点">
									<el-option v-for="item in sites" :key="item.id" :label="item.name" :value="item.id">
									</el-option>
								</el-select>
							</div>
							<div class="top_txt_list">
								<el-select clearable v-model="params.state" placeholder="发布状态">
									<el-option v-for="item in states" :key="item.id" :label="item.name"
										:value="item.id">
									</el-option>
								</el-select>
							</div>
							<div class="top_txt_list search">
								<el-input v-model="params.article_id" placeholder="文章ID"></el-input>
								<button @click="search">搜索</button>
							</div>
						</div>
					</div>
				</div>
				<div class="myarticles" v-loading="loading" element-loading-spinner="el-icon-loading"
					element-loading-text="拼命加载中" element-loading-background="rgba(255, 255, 255, 1)">
					<div class="myarticles_content creation">
						<div class="myarticles_content_title">
							<div class="content_title_list">序号</div>
							<div class="content_title_list">文章名称</div>
							<div class="content_title_list">分类</div>
							<div class="content_title_list">发布时间</div>
							<div class="content_title_list">站点</div>
							<div class="content_title_list">状态</div>
							<div class="content_title_list">所属计划</div>
							<div class="content_title_list">操作</div>
						</div>
						<div class="myarticles_content_tab">
							<div class="content_tab_list" :class="{ on: info.checked }"
								v-for="(info, index) in infos.data" :key="index">
								<div class="tab_list_li">{{ info.id }}</div>
								<div class="tab_list_li">{{ info.article_name }}</div>
								<div class="tab_list_li">{{ info.article_class_name }}</div>
								<div class="tab_list_li">{{ info.create_time }}</div>
								<div class="tab_list_li">{{ info.site_name }}</div>
								<div class="tab_list_li">
									<div class="list_li_state on" v-if="!info.state"><span></span>失败</div>
									<div class="list_li_state" v-if="info.state"><span></span>成功</div>
								</div>
								<div class="tab_list_li">{{ info.plan_name }}</div>
								<div class="tab_list_li operate">
									<div class="list_li_btn" @click="detail(index)"><img src="@a/img/sv9.svg" />查看</div>
								</div>
							</div>
							<div class="not_data not_data2" v-show="infos.data.length === 0">
								<div class="not_data_icon"><img src="@a/img/bg26.jpg" /></div>
								<div class="not_data_title">暂无发布日志</div>
							</div>
						</div>
					</div>
					<div class="page">
						<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
							:current-page="infos.current_page" :page-sizes="per_pages" :page-size="infos.per_page"
							layout="total, sizes, prev, pager, next, jumper" :total="infos.total">
						</el-pagination>
					</div>
				</div>
			</div>
		</div>


		<!-- 日志 -->
		<div class="Preview Edit Log" v-show="showLog">
			<div class="Preview_content">
				<div class="Preview_content_title">
					<div class="content_title_left"><span></span>日志</div>
					<div class="content_title_right" @click="showLog = !showLog"><i class="el-icon-close"></i></div>
				</div>
				<div class="Edit_content">
					<div class="Edit_content_list">
						<div class="content_list_dd">日志ID</div>
						<div class="content_list_dt">{{ log.id }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">文章ID</div>
						<div class="content_list_dt">{{ log.article_id }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">标题</div>
						<div class="content_list_dt">{{ log.article_name }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">分类</div>
						<div class="content_list_dt">{{ log.article_class_name }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">发布时间</div>
						<div class="content_list_dt">{{ log.create_time }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">发布状态</div>
						<div class="content_list_dt"><span>{{ log.state ? '成功' : '失败' }}</span></div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">所属计划</div>
						<div class="content_list_dt">{{ log.plan_name }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">站点</div>
						<div class="content_list_dt">{{ log.site_name }}</div>
					</div>
					<div class="Edit_content_list">
						<div class="content_list_dd">站点分类ID</div>
						<div class="content_list_dt">{{ log.site_catid }}</div>
					</div>
          <div class="Edit_content_list">
            <div class="content_list_dd">备注</div>
            <div class="content_list_dt">{{ log.remarks ? log.remarks : '-' }}</div>
          </div>
				</div>
			</div>
		</div>

	</div>
</template>

<script>
	import Nav from "@c/common/nav/nav";

	function getDateStrings() {
		// 创建一个 Date 对象
		const date = new Date();

		// 获取年份、月份、日期
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');

		// 将当前日期拼接为字符串
		const today = `${year}-${month}-${day}`;

		// 将昨天的日期计算出来
		date.setDate(date.getDate() - 1);
		const yesterdayYear = date.getFullYear();
		const yesterdayMonth = String(date.getMonth() + 1).padStart(2, '0');
		const yesterdayDay = String(date.getDate()).padStart(2, '0');

		// 将昨天的日期拼接为字符串
		const yesterday = `${yesterdayYear}-${yesterdayMonth}-${yesterdayDay}`;

		// 返回结果
		return {
			today,
			yesterday
		};
	}

	const {
		today,
		yesterday
	} = getDateStrings();

	export default {
		name: "plantorelease",
		data() {
			return {
				showLog: false,
				log: {

				},
				states: [{
						id: 1,
						name: '发布成功',
					},
					{
						id: 0,
						name: '发布失败',
					},
				],
				sites: [],
				plans: [],
				class_types: [],
				isChecked: false,
				showPreview: false,
				infos: {
					data: [],
					per_page: 10,
					current_page: 1,
					last_page: 1,
					total: 0
				},
				params: {
					time: this.$route.query.time == 1 ? [today, today] : (this.$route.query.time == -1 ? [yesterday,
						yesterday] : ['', '']),
					article_class_id: '',
					push_plan_id: this.$route.query.push_plan_id ? parseInt(this.$route.query.push_plan_id) : '',
					site_id: this.$route.query.site_id ? parseInt(this.$route.query.site_id) : '',
					state: '',
					article_id: ''
				},
				per_pages: [10, 20, 30],
        loading: true,
			};
		},
		components: {
			Nav
		},

		methods: {
      search(){
        this.infos.current_page = 1;
        this.getList()
      },
			detail(index) {
				this.log = this.infos.data[index];
				this.showLog = true;
			},
			handleSizeChange(val) {
				this.infos.per_page = val
				this.getList()
			},
			handleCurrentChange(val) {
				this.infos.current_page = val
				this.getList()

			},
			getList() {
				// loading 开始
        this.loading = true;

				let that = this;
				let this_params = JSON.parse(JSON.stringify(that.params));
				this_params.start = this_params.time[0]
				this_params.end = this_params.time[1]
				delete this_params.time;
				for (let i in this_params) {
					if (this_params[i] === '') {
						delete this_params[i];
					}
				}
				let params = {
					...this_params,
					page: that.infos.current_page,
					keywords: that.keywords,
					list_rows: that.infos.per_page,
					class_id: that.class_type,
				}

				that.axios.get('/api/pushLog/list', {
					params
				}).then(res => {
					// loading结束
          this.loading = false;

					that.infos = res.result;
				})
			},
			getPlans() {
				let that = this;
				this.axios.get('/api/plan/list', {
					list_row: 100000
				}).then(res => {
					that.plans = res.result.data
					that.params = JSON.parse(JSON.stringify(that.params))
				})
			},
			getArticleClass() {
				let that = this;
				this.axios.get('/api/articleClass/list').then(res => {
					res.result.forEach(item => {
						item.isActive = false
					})
					that.class_types = res.result
				})
			},


			getSites() {
				let that = this;
				this.axios.get('/api/site/list').then(res => {

					that.sites = res.result.data
					that.params = JSON.parse(JSON.stringify(that.params))
				})
			},
		},
		created() {
			this.getArticleClass()
			this.getSites()
			this.getPlans()
			this.getList()

		},
	}
</script>

<style>
	@import "releaselog.css";
</style>
